package com.jason.apps.fromdashboard;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.apps.realtyportal.HomeActivity;
import com.apps.realtyportal.MyFragmentPagerAdapterAgent;
import com.apps.realtyportal.R;
import com.jason.apps.fromdashboard.ForRent;
import com.jason.apps.fromdashboard.ForSale;
import com.shanu.apps.details.ShowPropertyDetail;

@SuppressLint({ "NewApi", "NewApi", "ParserError" })
public class Agent extends SherlockFragmentActivity implements
		ForRent.RentOnClickSelectedListener,
		ForSale.SaleOnClickSelectedListener,
		MyProperty.MyOnClickSelectedListener {

	public static String Path = null;
	public static String DATE = null;
	private static final int ID_LOGOUT = 1;
	public static Context appContext;
	ActionBar actionbar;
	public String whoserunning;

	FragmentManager fm;

	ViewPager mPager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agent_fragment);
		appContext = getApplicationContext();

		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setDisplayShowTitleEnabled(false);

		/** Getting a reference to action bar of this activity */
		// actionbar = getSupportActionBar();

		/** Set tab navigation mode */
		actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		/** Getting a reference to ViewPager from the layout */
		mPager = (ViewPager) findViewById(R.id.pager);

		/** Getting a reference to FragmentManager */
		fm = getSupportFragmentManager();

		/** Defining a listener for pageChange */
		ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				actionbar.setSelectedNavigationItem(position);
			}

		};

		/** Setting the pageChange listner to the viewPager */
		mPager.setOnPageChangeListener(pageChangeListener);

		/** Creating an instance of FragmentPagerAdapter */
		MyFragmentPagerAdapterAgent fragmentPagerAdapter = new MyFragmentPagerAdapterAgent(
				fm);

		/** Setting the FragmentPagerAdapter object to the viewPager object */
		mPager.setAdapter(fragmentPagerAdapter);

		actionbar.setDisplayShowTitleEnabled(true);

		/** Defining tab listener */
		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			public Fragment fragment;

			public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			}

			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				mPager.setCurrentItem(tab.getPosition());
				// ft.replace(R.id.buyer_fragment, fragment);
			}

			public void onTabReselected(Tab tab, FragmentTransaction ft) {

				fm.popBackStack();
			}
		};

		/** Creating Android Tab */
		Tab tab = actionbar.newTab().setText("For Rent")
				.setIcon(R.drawable.editpropxml).setTabListener(tabListener);

		actionbar.addTab(tab);

		/** Creating Apple Tab */
		tab = actionbar.newTab().setText("For Sale")
				.setIcon(R.drawable.forsalexml).setTabListener(tabListener);

		actionbar.addTab(tab);

		/** Creating Apple Tab */
		tab = actionbar.newTab().setText("My Property")
				.setIcon(R.drawable.mypropertyxml).setTabListener(tabListener);

		actionbar.addTab(tab);

		/** Creating Apple Tab */
		tab = actionbar.newTab().setText("Buyer").setIcon(R.drawable.buyerxml)
				.setTabListener(tabListener);

		actionbar.addTab(tab);

		/*
		 * actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		 * 
		 * ActionBar.Tab sale =
		 * actionbar.newTab().setText("For Sale").setIcon(R.
		 * drawable.allproperty); ActionBar.Tab rent =
		 * actionbar.newTab().setText
		 * ("For Rent").setIcon(R.drawable.allproperty); ActionBar.Tab
		 * myproperty =
		 * actionbar.newTab().setText("My Property").setIcon(R.drawable
		 * .myproperty); ActionBar.Tab buyer =
		 * actionbar.newTab().setText("Leads").setIcon(R.drawable.buyer);
		 * 
		 * Fragment saleFragment = new ForSale(); Fragment rentFragment = new
		 * ForRent(); Fragment EditFragment = new MyProperty(); Fragment
		 * DeleteFragment = new PropertyLeads();
		 * 
		 * sale.setTabListener(new MyTabsListener(saleFragment));
		 * rent.setTabListener(new MyTabsListener(rentFragment));
		 * myproperty.setTabListener(new MyTabsListener(EditFragment));
		 * buyer.setTabListener(new MyTabsListener(DeleteFragment));
		 * 
		 * actionbar.addTab(rent); actionbar.addTab(sale);
		 * actionbar.addTab(myproperty); actionbar.addTab(buyer);
		 */

	}

	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

		// /* Add an expandable Search Icon in the ActionBar */
		// menu.add(0, ID_LOGOUT, 0, "Search").setIcon(R.drawable.logouticon)
		// .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		//
		// return super.onCreateOptionsMenu(menu);

		SubMenu subMenu = menu.addSubMenu("Options");

		// subMenu.add("Logout").setIcon(R.drawable.logouticon);
		// subMenu.add("Go to Profile").setIcon(R.drawable.profile1);

		subMenu.add(0, 10, Menu.NONE, "Logout").setIcon(R.drawable.logouticon);
		subMenu.add(0, 15, Menu.NONE, "Back to Dashboard").setIcon(
				R.drawable.profile);

		MenuItem subMenu1Item = subMenu.getItem();
		subMenu1Item.setIcon(R.drawable.ic_action_overflow);
		subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

		return super.onCreateOptionsMenu(menu);
	}

	public void MyonArticleSelected(View v, int position) {

		FragmentManager fm = getSupportFragmentManager();

		if (fm != null) {

			// Perform the FragmentTransaction to load in the list tab
			// content.
			// Using FragmentTransaction#replace will destroy any Fragments
			// currently inside R.id.fragment_content and add the new
			// Fragment
			// in its place.
			// FragmentTransaction ft = fm.beginTransaction();
			// ft.replace(android.R.id.content, new MapFragment());
			// ft.commit();

			Fragment productDetailFragment = new ShowPropertyDetail();
			Bundle args = new Bundle();
			args.putInt(ShowPropertyDetail.ARG_POSITION, position);
			args.putString("running", "myprop");
			productDetailFragment.setArguments(args);
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();
			transaction.addToBackStack(null);
			transaction.replace(android.R.id.content, productDetailFragment)
					.commit();
		}

		// ShowPropertyDetail newFragment = new ShowPropertyDetail();
		// Bundle args = new Bundle();
		// args.putInt(ShowPropertyDetail.ARG_POSITION, position);
		// args.putString("running", "myprop");
		// newFragment.setArguments(args);
		// FragmentTransaction transaction = getSupportFragmentManager()
		// .beginTransaction();
		// transaction.replace(R.id.agent_fragment, newFragment);
		// transaction.addToBackStack(null);
		// transaction.commit();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, HomeActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);

		case 10:

			finish();
			HomeActivity.islogin = false;
			Intent intent_logout = new Intent(this, HomeActivity.class);
			intent_logout.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent_logout);
		case 15:
			finish();

		}
		return super.onOptionsItemSelected(item);
	}

	public void SaleonArticleSelected(View v, int position) {
		// TODO Auto-generated method stub
		// ShowPropertyDetail newFragment = new ShowPropertyDetail();
		// Bundle args = new Bundle();
		// args.putInt(ShowPropertyDetail.ARG_POSITION, position);
		// args.putString("running", "sale");
		// newFragment.setArguments(args);
		// FragmentTransaction transaction = getSupportFragmentManager()
		// .beginTransaction();
		// transaction.replace(R.id.agent_fragment, newFragment);
		// transaction.addToBackStack(null);
		// transaction.commit();

		FragmentManager fm = getSupportFragmentManager();

		if (fm != null) {

			// Perform the FragmentTransaction to load in the list tab
			// content.
			// Using FragmentTransaction#replace will destroy any Fragments
			// currently inside R.id.fragment_content and add the new
			// Fragment
			// in its place.
			// FragmentTransaction ft = fm.beginTransaction();
			// ft.replace(android.R.id.content, new MapFragment());
			// ft.commit();

			Fragment productDetailFragment = new ShowPropertyDetail();
			Bundle args = new Bundle();
			args.putInt(ShowPropertyDetail.ARG_POSITION, position);
			args.putString("running", "sale");
			productDetailFragment.setArguments(args);
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();
			transaction.addToBackStack(null);
			transaction.replace(android.R.id.content, productDetailFragment)
					.commit();
		}
	}

	public void RentArticleSelected(View v, int position) {
		// TODO Auto-generated method stub

		FragmentManager fm = getSupportFragmentManager();

		if (fm != null) {

			// Perform the FragmentTransaction to load in the list tab
			// content.
			// Using FragmentTransaction#replace will destroy any Fragments
			// currently inside R.id.fragment_content and add the new
			// Fragment
			// in its place.
			// FragmentTransaction ft = fm.beginTransaction();
			// ft.replace(android.R.id.content, new MapFragment());
			// ft.commit();

			Fragment productDetailFragment = new ShowPropertyDetail();
			Bundle args = new Bundle();
			args.putInt(ShowPropertyDetail.ARG_POSITION, position);
			args.putString("running", "rent");
			productDetailFragment.setArguments(args);
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();
			transaction.addToBackStack(null);
			transaction.replace(android.R.id.content, productDetailFragment)
					.commit();
		}

		/*
		 * Fragment newFragment = new ShowPropertyDetail(); Bundle args = new
		 * Bundle(); args.putInt(ShowPropertyDetail.ARG_POSITION, position);
		 * args.putString("running", "rent"); newFragment.setArguments(args);
		 * FragmentTransaction transaction = getSupportFragmentManager()
		 * .beginTransaction(); transaction.replace(R.id.agent_fragment,
		 * newFragment); transaction.addToBackStack(null); transaction.commit();
		 */
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
	}

}

/*
 * @SuppressLint("ParserError") class MyTabsListener implements
 * ActionBar.TabListener { public Fragment fragment;
 * 
 * public MyTabsListener(Fragment fragment) { this.fragment = fragment; }
 * 
 * public void onTabReselected(Tab tab, FragmentTransaction ft) {
 * //ft.attach(fragment); FragmentManager fm=this.fragment.getFragmentManager();
 * fm.popBackStack();
 * 
 * }
 * 
 * public void onTabSelected(Tab tab, FragmentTransaction ft) {
 * ft.replace(R.id.agent_fragment, fragment); }
 * 
 * public void onTabUnselected(Tab tab, FragmentTransaction ft) {
 * //ft.detach(fragment); }
 */
